package fr.epsi.spring1.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import fr.epsi.spring1.dao.ArticleDao;
import fr.epsi.spring1.domain.Article;

public class ArticleDummyImpl implements ArticleDao {

	private Map<String, Article> mapOfArticles = new HashMap<String,Article>();
	
	public ArticleDummyImpl() {
		
		Article article1 = new Article();
		article1.setReference("1");
		article1.setNom("iPad");
		article1.setDescription("tablette");
		
		mapOfArticles.put(article1.getReference(), article1);
		
	}
	@Override
	public boolean insert(Article article) {
		if (mapOfArticles.containsKey(article.getReference())) {
		return false;
		} else {
			mapOfArticles.put(article.getReference(), article);
			return true;
		}
	}

	@Override
	public void update(Article article) {
		if (mapOfArticles.containsKey(article.getReference())) {
			mapOfArticles.put(article.getReference(), article);
		}
	}

	@Override
	public List<Article> select(String reference) {
		List<Article> listOfArticles = new ArrayList<Article>();
		listOfArticles.addAll(mapOfArticles.values());
		return listOfArticles;
	}

	@Override
	public Article getArticle(String reference) {
		return mapOfArticles.get(reference);
	}
	@Override
	public Double getNbArticles() {
		return Double.valueOf(mapOfArticles.values().size());
	}

}
